
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_height: 0,
    setting_btn_height: 0,
    role: 0,// 1:学生 2: 老师， 3:管理员 4: 校友
    sex_array: ["请选择", "女", "男"],
    sex_index: 0,
    school: "",
    school_list: [],
    school_array: [],
    school_index: 0,
    password_one: "",
    password_two: "",
    name: "",
    id: "",
    classNo: "",
    grad_year: "1955",
    birthday: '2016-09-01',
    phone: "",
    email: "",
    signup_btn: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(getApp().globalData.school_list)

    var school_array_temp1 = getApp().globalData.school_list
    var school_array_temp2 = []
    for (var i = 0; i < school_array_temp1.length; i++) {
      school_array_temp2.push(school_array_temp1[i].name)
      console.log(school_array_temp2)
    }
    school_array_temp2.unshift("请选择")

    console.log(school_array_temp2)
    this.setData({
      list_height: getApp().globalData.windowHeight * 0.85,
      setting_btn_height: getApp().globalData.windowHeight * 0.1,
      role: options.type,
      school_list: getApp().globalData.school_list,
      school_array: school_array_temp2
    })
  },

  miss: function () {
    var that = this
    wx.hideLoading()
    that.setData({
      signup_btn: false
    })
    wx.showModal({
      title: '哎呀～',
      content: "有的选项您忘填了呢～",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  add: function () {
    var that = this

    that.setData({
      signup_btn: true
    })
    wx.showLoading({
      title: '注册中',
    })

    if (that.data.role == "5") {
      //增加学校
      console.log(that.data.school)
      wx.request({
        url: getApp().globalData.my_server + "/login/signup",
        data: {
          kind: "5",
          school: that.data.school
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res)
          if (res.data.error_no == 2) {
            wx.hideLoading()
            that.setData({
              signup_btn: false
            })
            wx.showModal({
              title: '哎呀～',
              content: res.data.error_msg,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else if (res.data.error_no == 0) {
            //查询学校列表
            wx.request({
              url: getApp().globalData.my_server + "/login/list_school",
              data: {},
              method: "POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              success: function (res) {
                console.log(res)
                if (res.data.error_no == 2) {
                  wx.hideLoading()
                  that.setData({
                    signup_btn: false
                  })
                  wx.showModal({
                    title: '哎呀～',
                    content: res.data.error_msg,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                } else if (res.data.error_no == 0) {
                  wx.hideLoading()

                  getApp().globalData.school_list = res.data.document_array
                  console.log(getApp().globalData.school_list)
                  wx.navigateBack({
                    delta: 1
                  })
                }
              },
              fail: function (res) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: '查询学校列表失败',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
                console.log("fail!", res)
              },
              complete: function (res) {

              }
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          that.setData({
            signup_btn: false
          })
          wx.showModal({
            title: '哎呀～',
            content: '编辑失败',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          console.log("fail!", res)
        },
        complete: function (res) {

        }
      })
    } else {
      if (that.data.name == "" || that.data.id == "") {
        that.miss()
      } else if (that.data.role == "1" && that.data.classNo == "") {
        that.miss()
      } else if ((that.data.role == "2") && (that.data.password_one == "" || that.data.password_two == "")) {
        that.miss()
      } else if ((that.data.role == "3") && (that.data.password_one == "" || that.data.password_two == "")) {
        that.miss()
      } else if ((that.data.role == "4") && (that.data.birthday == "" || that.data.phone == "" || that.data.email == "")) {
        that.miss()
      } else {
        //查询ID是否已经被注册
        wx.request({
          url: getApp().globalData.my_server + "/login/checkId",
          data: {
            key: that.data.id
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res)
            if (res.data.error_no == 2) {
              wx.hideLoading()
              that.setData({
                signup_btn: false
              })
              wx.showModal({
                title: '哎呀～',
                content: res.data.error_msg,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            } else if (res.data.error_no == 0) {
              if (that.data.sex_index == 0) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "请选择性别～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (that.data.school_index == 0) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "请选择学校～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if ((that.data.role == "1") && isNaN(parseInt(that.data.classNo))) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "班级号格式错误～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if ((that.data.role == "1") && isNaN(parseInt(that.data.id))) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "ID格式错误～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if ((that.data.role == "4") && !(/^1[34578]\d{9}$/).test(that.data.phone)) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "手机号格式错误～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if ((that.data.role == "4") && !(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(that.data.email)) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "邮箱格式错误～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if ((that.data.role == "3" || that.data.role == "2") && that.data.password_one != that.data.password_two) {
                wx.hideLoading()
                that.setData({
                  signup_btn: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: "两次密码输入不一样诶～",
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else {
                wx.login({
                  success: function (res_code) {
                    if (res_code.code) {

                      var data = {}
                      var sex = "unknown"
                      if (that.data.sex_index == 1) {
                        sex = "female"
                      } else if (that.data.sex_index == 2) {
                        sex = "male"
                      }

                      console.log(getApp().globalData.code)
                      if (that.data.role == "1") {
                        data = {
                          code: res_code.code,
                          kind: "1",
                          studentNo: that.data.id,
                          classNo: that.data.classNo,
                          name: that.data.name,
                          userInfo: getApp().globalData.userInfo,
                          sex: sex,
                          school: that.data.school_array[that.data.school_index]
                        }
                      } else if (that.data.role == "2") {
                        data = {
                          code: res_code.code,
                          kind: "2",
                          name: that.data.name,
                          teacherNo: that.data.id,
                          userInfo: getApp().globalData.userInfo,
                          sex: sex,
                          school: that.data.school_array[that.data.school_index],
                          password: that.data.password_one
                        }
                      } else if (that.data.role == "3") {
                        data = {
                          code: res_code.code,
                          kind: "3",
                          name: that.data.name,
                          adminNo: that.data.id,
                          userInfo: getApp().globalData.userInfo,
                          sex: sex,
                          school: that.data.school_array[that.data.school_index],
                          password: that.data.password_one
                        }
                      } else if (that.data.role == "4") {
                        data = {
                          code: res_code.code,
                          kind: "4",
                          donatorNo: that.data.id,
                          name: that.data.name,
                          sex: sex, //0 is male, 1 is female
                          birthday: that.data.birthday,
                          phone: that.data.phone,
                          email: that.data.email,
                          userInfo: getApp().globalData.userInfo,
                          grad_school: that.data.school_array[that.data.school_index],
                          year: that.data.grad_year
                        }
                      }

                      //注册
                      wx.request({
                        url: getApp().globalData.my_server + "/login/signup",
                        data: data,
                        method: "POST",
                        header: {
                          "Content-Type": "application/x-www-form-urlencoded"
                        },
                        success: function (res) {
                          console.log(res)
                          if (res.data.error_no == 2) {
                            wx.hideLoading()
                            that.setData({
                              signup_btn: false
                            })
                            wx.showModal({
                              title: '哎呀～',
                              content: res.data.error_msg,
                              success: function (res) {
                                if (res.confirm) {
                                  console.log('用户点击确定')
                                } else if (res.cancel) {
                                  console.log('用户点击取消')
                                }
                              }
                            })
                          } else if (res.data.error_no == 0) {
                            wx.hideLoading()

                            if (that.data.role == "2") {
                              wx.navigateBack({
                                delta: 1
                              })
                            } else if (that.data.role != "2") {
                              wx.reLaunch({
                                url: '../applications/applications?type=' + that.data.role,
                              })
                            }
                          }
                        },
                        fail: function (res) {
                          wx.hideLoading()
                          that.setData({
                            signup_btn: false
                          })
                          wx.showModal({
                            title: '哎呀～',
                            content: '注册失败',
                            success: function (res) {
                              if (res.confirm) {
                                console.log('用户点击确定')
                              } else if (res.cancel) {
                                console.log('用户点击取消')
                              }
                            }
                          })
                          console.log("fail!", res)
                        },
                        complete: function (res) {

                        }
                      })


                    } else {
                      console.log('登录失败！' + res.errMsg)
                    }
                  }
                });

              }
            }
          },
          fail: function (res) {
            wx.hideLoading()
            that.setData({
              signup_btn: false
            })
            wx.showModal({
              title: '哎呀～',
              content: '获取信息失败',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
            console.log("fail!", res)
          },
          complete: function (res) {

          }
        })

      }
    }

  },

  school_input: function (e) {
    this.setData({
      school: e.detail.value
    })
  },

  email_input: function (e) {
    this.setData({
      email: e.detail.value
    })
  },

  phone_input: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  bindSchoolChange: function (e) {
    this.setData({
      school_index: e.detail.value
    })
  },

  bindBirthdayChange: function (e) {
    this.setData({
      birthday: e.detail.value
    })
  },

  bindYearChange: function (e) {
    this.setData({
      grad_year: e.detail.value
    })
  },

  class_input: function (e) {
    this.setData({
      classNo: e.detail.value
    })
  },

  name_input: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  id_input: function (e) {
    this.setData({
      id: e.detail.value
    })
  },

  bindSexChange: function (e) {
    this.setData({
      sex_index: e.detail.value
    })
  },

  input_password_one: function (e) {
    this.setData({
      password_one: e.detail.value
    })
  },

  input_password_two: function (e) {
    this.setData({
      password_two: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})